package com.rpj.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.rpj.dao.ModulesDao;
import com.rpj.entity.ModulesDto;
import com.rpj.entity.ModulesEntity;
import com.rpj.service.ModulesService;
@Service
public class ModulesImpl implements ModulesService{
@Autowired
public ModulesDao modulesdao;
//添加
@Override
public int saveModules(ModulesEntity modules) {
	// TODO Auto-generated method stub
	return modulesdao.saveModules(modules);
}
//修改
@Override
public int updataModules(ModulesEntity modules) {
	// TODO Auto-generated method stub
	return modulesdao.updataModules(modules);
}
//删除
@Override
public int delModules(ModulesEntity modules) {
	// TODO Auto-generated method stub
	return modulesdao.delModules(modules);
}
@Override
public List<ModulesEntity> findModules() {
	// TODO Auto-generated method stub
	return modulesdao.findModules();
}
public List<ModulesDto> treeModules(){
	ModulesDto dto = new ModulesDto();
	List<ModulesEntity> list = findModules();
	ArrayList<ModulesDto> arrayList = new ArrayList<ModulesDto>();
	for (ModulesEntity i : list) {
		
		dto.setId(i.getId());
		dto.setPid(i.getPid());
		dto.setTitle(i.getTitle());
		dto.setHref(i.getPath());
		dto.setChecked(false);
		arrayList.add(dto);
	}
	return arrayList;
}
@Override
public List<ModulesDto> loadTree() {
	List<ModulesDto> list = treeModules();
	
	//声明一个集合存根节点
	List<ModulesDto> arrayList = new ArrayList<ModulesDto>();
	
	
	for(ModulesDto dto:list) {
		if(dto.getPid()==0) {
			arrayList.add(getchildren(list,dto));
		}
	}
	return arrayList;
}
//获取子节点的方法
	public ModulesDto getchildren(List<ModulesDto> rootList,ModulesDto dto) {
		//声明一个集合存储子类节点
		List<ModulesDto> childrenList = new ArrayList<ModulesDto>();
		for(ModulesDto tree:rootList) {
			if(tree.getPid()==dto.getId()) {
				if(dto.getChildren()==null) {
					dto.setChildren(childrenList);
				}
				dto.getChildren().add(getchildren(rootList,tree));
			}
		}
		return dto;
	}
	@Override
	public List<ModulesEntity> selByRoleName(List<String> roleNames) {
		// TODO Auto-generated method stub
		return modulesdao.selByRoleName(roleNames);
	}
	//将查询的模块信息封装成树节点
		public List<ModulesDto> changeTreeNode(List<String> name){
				//获取模块信息
			List<ModulesEntity> list = selByRoleName(name);
			System.out.println("list=>"+list);
			 //声明树节点集合
			 List<ModulesDto> treeList = new ArrayList<ModulesDto>();
			 for(ModulesEntity module:list) {
				 ModulesDto tree = new ModulesDto();
				 tree.setId(module.getId());
				 tree.setTitle(module.getTitle());
				 tree.setPid(module.getPid());
				 tree.setChecked(false);
				 tree.setHref(module.getPath());
				 treeList.add(tree);
			 }
			 return treeList;
		}
	public List<ModulesDto> getsysRight(List<String> roleNames) {
		List<ModulesDto> list = changeTreeNode(roleNames);
		System.out.println("转换之后list=>"+list);
		//声明一个集合存根节点
				List<ModulesDto> rootList = new ArrayList<ModulesDto>();
				for(ModulesDto tree:list) {
					if(tree.getPid()==0) {
						rootList.add(getchildren(list,tree));
					}
				}
				return rootList;
	}
}
